package com.lemon.smartsafety.ui.activity.user;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lemon.android.http.AesEncryptor;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.UserServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.KeyBoardUtils;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Cooliean on 16/4/9.
 */
public class ModifyPasswordActivity extends SwipeBackActivity {
    @Bind(R.id.metOldPassword)
    MaterialEditText metOldPassword;
    @Bind(R.id.metPassword)
    MaterialEditText metPassword;
    @Bind(R.id.metConfirmPassword)
    MaterialEditText metConfirmPassword;
    @Bind(R.id.btn_submit)
    Button           btnSubmit;

    @Override
    protected void initTitle() {
        setBarTitle(R.string.draw_password);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(ModifyPasswordActivity.this);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected int getContentViewId() {
        return R.layout.activity_modify_pwd;
    }


    @OnClick({R.id.btn_submit})
    public void submitAction(View view) {
        if (!viladateForm()) {
            return;
        }

        getConfirmDialog("是否提交?", new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.POSITIVE) {
                    doSubmitAction();
                }
            }
        }).show();
    }

    private boolean viladateForm() {
        if (TextUtils.isEmpty(metOldPassword.getText().toString())) {
            metOldPassword.setError("请填写旧密码");
            mHUDUtil.showErrorTip(metOldPassword.getError().toString());
            return false;
        }

        if (TextUtils.isEmpty(metPassword.getText().toString())) {
            metPassword.setError("请填写新密码");
            mHUDUtil.showErrorTip(metPassword.getError().toString());
            return false;
        }

        String password        = StringUtils.nullValue(metPassword.getText().toString());
        String passwordConfirm = StringUtils.nullValue(metConfirmPassword.getText().toString());

        if (!passwordConfirm.equals(password)) {
            mHUDUtil.showErrorTip("两次密码不一致");
            return false;
        }
        return true;
    }

    private void doSubmitAction() {
        LemonRequestParams params = new LemonRequestParams();
        String old_password=metOldPassword.getText().toString();
        String old_password_encrypt=AesEncryptor.encrypt(old_password);
        Log.i("old_password=",old_password);
        Log.i("old_password_encrypt=",old_password_encrypt);
        params.put("old_password",old_password_encrypt);
        params.put("new_password", AesEncryptor.encrypt(metPassword.getText().toString()));
        UserServices.init().doModifyPassword(params);
    }

    /**
     * http处理请求的处理
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(HttpCallbackEvent event) {
        if (EventTag.TAG_MODIFY_PASSWORD.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.showInfoTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                }
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_MODIFY_PASSWORD);
        KeyBoardUtils.closeSoftInput(this);
    }
}
